package com.hzdesk.media.koreabatchmatvector.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFile;
import com.hzdesk.media.koreabatchmatvector.logic.persenter.TimeLinePersenter;
import com.hzdesk.media.koreabatchmatvector.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/7/17.
 * 时间轴适配器
 */

public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineAdapter.MyTimeLineViewHoder> {
    private Context mContext;
    private List<TimeLinePersenter.MyTimeLineBean> timeLineBeanList;
    private onItemOnClickListener onItemOnClickListener;


    public TimeLineAdapter(Context mContext, List<TimeLinePersenter.MyTimeLineBean> mList) {
        this.mContext = mContext;
        timeLineBeanList = mList;

    }

    public void setOnItemOnClickListener(TimeLineAdapter.onItemOnClickListener onItemOnClickListener) {
        this.onItemOnClickListener = onItemOnClickListener;
    }

    public void setDate(List<TimeLinePersenter.MyTimeLineBean> mList){
        timeLineBeanList = mList;
        notifyDataSetChanged();
    }


    @Override
    public TimeLineAdapter.MyTimeLineViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_line_item, parent, false);
        MyTimeLineViewHoder myTimeLineViewHoder = new MyTimeLineViewHoder(view);
        return myTimeLineViewHoder;
    }

    @Override
    public void onBindViewHolder(TimeLineAdapter.MyTimeLineViewHoder holder, final int position) {
        final TimeLinePersenter.MyTimeLineBean bean = timeLineBeanList.get(position);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
        String time_day = simpleDateFormat.format(new Date(bean.getTime()));
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
        String time_hour = simpleDateFormat1.format(new Date(bean.getTime()));
        holder.item_time_week.setText(DateUtil.getWeek(new Date(bean.getTime())) + " " + time_hour);
        holder.item_tiem_day.setText(time_day);
        holder.item_time_ch.setText(DateUtil.getMonth(new Date(bean.getTime())) + "月");
        holder.item_time_en.setText(DateUtil.getMonthEnglish(new Date(bean.getTime())));
        if (position == 0) {
            holder.item_time_ch.setVisibility(View.VISIBLE);
            holder.item_time_en.setVisibility(View.VISIBLE);
            holder.item_imgage_point.setVisibility(View.VISIBLE);

        } else {
            if (bean.getmonth() == timeLineBeanList.get(position - 1).getmonth()) {
                holder.item_time_ch.setVisibility(View.GONE);
                holder.item_time_en.setVisibility(View.GONE);
                holder.item_imgage_point.setVisibility(View.GONE);
            } else {
                holder.item_time_ch.setVisibility(View.VISIBLE);
                holder.item_time_en.setVisibility(View.VISIBLE);
                holder.item_imgage_point.setVisibility(View.VISIBLE);
            }
        }

        final String uuid=bean.getUuid();
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemOnClickListener != null) {
                    onItemOnClickListener.onItemClick(uuid);
                }

            }
        });
        holder.linearLayout.setOnHoverListener(onHoverListener);
        holder.linearLayout.setOnFocusChangeListener(onFocusChangeListener);


    }

    @Override
    public int getItemCount() {
        return timeLineBeanList.size();
    }

    class MyTimeLineViewHoder extends RecyclerView.ViewHolder {
        ImageView item_imgage_point;
        TextView item_time_ch;
        TextView item_time_en;
        TextView item_tiem_day;
        TextView item_time_week;
        LinearLayout linearLayout;

        public MyTimeLineViewHoder(View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.time_line_item_li);
            item_imgage_point = itemView.findViewById(R.id.time_line_item_image);
            item_time_ch = itemView.findViewById(R.id.time_line_item_tv_time_ch);
            item_time_en = itemView.findViewById(R.id.time_line_item_tv_time_en);
            item_tiem_day = itemView.findViewById(R.id.time_line_item_tv_time_day);
            item_time_week = itemView.findViewById(R.id.time_line_item_tv_time_week);
        }
    }


    public interface onItemOnClickListener {
        void onItemClick(String uuid);
    }


    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    //v.clearFocus();
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };

    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LinearLayout linearLayout = (LinearLayout) v;
            if (hasFocus) {
                linearLayout.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            } else {
                linearLayout.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };

}
